﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.IO.IsolatedStorage;

namespace AdvancedStandingAndScholarship
{
    public partial class AddPageSelectSchool : Page
    {
        public AddPageSelectSchool()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ASASService.ASASServiceClient client = new ASASService.ASASServiceClient();
            client.getAllCountryCompleted += new EventHandler<ASASService.getAllCountryCompletedEventArgs>(client_getAllCountryCompleted);
            client.getAllCountryAsync();
        }

        void client_getAllCountryCompleted(object sender, ASASService.getAllCountryCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                cbx_country.ItemsSource = e.Result as List<string>;
            }
        }

        private void cbx_country_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            if (cbx_country.SelectedItem != null)
            {
                ASASService.ASASServiceClient client = new ASASService.ASASServiceClient();
                client.getSelectedSchoolCompleted += new EventHandler<ASASService.getSelectedSchoolCompletedEventArgs>(client_getSelectedSchoolCompleted);
                client.getSelectedSchoolAsync(cbx_country.SelectedItem.ToString());
            }
        }

        void client_getSelectedSchoolCompleted(object sender, ASASService.getSelectedSchoolCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                cbx_uniName.ItemsSource = e.Result as List<string>;
            }
        }

        private void btnNext_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            if (cbx_uniName.SelectedValue == null || cbx_country.SelectedValue == null)
            {
                MessageBox.Show("Please select the school before proceeding");   
            }
            else
            {
                IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;

                // Add a key and its value.
                userSettings.Remove("selectedSchool");
                userSettings.Add("selectedSchool", cbx_uniName.SelectedValue.ToString());

                //// Remove the setting.
                //userSettings.Remove("userImage");
                NavigationService.Navigate(new Uri("/AddPage", UriKind.Relative));
            }
           
        }

        private void btnBack_Add_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            NavigationService.Navigate(new Uri("/MainPage", UriKind.Relative));
        }

        private void cbx_country_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }


    }
}
